package com.itheima.service.impl;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.annotation.OpLog;
import com.itheima.domain.PageResult;
import com.itheima.domain.StudentQueryParam;
import com.itheima.domain.pojo.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> pageStudents(StudentQueryParam param) {
        //开启分页
        PageHelper.startPage(param.getPage(), param.getPageSize());

        //调用mapper方法
        List<Student> rows =studentMapper.selectStudents(param);
        Page<Student> p = (Page<Student>) rows;

        return new PageResult<>(p.getTotal(), p.getResult());
    }

    @Override
    @OpLog
    public void delectByIds(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Override
    @OpLog
    public void insertStudent(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insertStudent(student);
    }

    /**
     * 根据id查询学生信息
     * @param id
     * @return
     */
    @Override
    public Student getById(Integer id) {
         return studentMapper.selectById(id);
    }


    /**
     * 根据id修改学生信息
     * @param student
     */
    @Override
    @OpLog
    public void updateById(Student student) {
        studentMapper.updateById(student);
    }

    /**
     * 根据id修改学生违规次数和违规分数
     * @param id
     * @param score
     */
    @Override
    @OpLog
    public void UpdateViolationScore(Integer id, Integer score) {
        studentMapper.UpdateViolationScore(id, score);
    }
}
